package com.yixia.pea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixia.common.annotation.DataScope;
import com.yixia.common.core.domain.entity.SysUser;
import com.yixia.common.exception.ServiceException;
import com.yixia.common.utils.SecurityUtils;
import com.yixia.common.utils.StringUtils;
import com.yixia.pea.constant.FinishStatus;
import com.yixia.pea.domain.credit.PeaScore;
import com.yixia.pea.domain.dto.PeaHzBaseDto;
import com.yixia.pea.domain.dto.PeaScoreDto;
import com.yixia.pea.service.PeaScoreService;
import com.yixia.pea.mapper.PeaScoreMapper;
import com.yixia.system.mapper.system.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 35038
* @description 针对表【pea_score】的数据库操作Service实现
* @createDate 2023-09-05 21:01:59
*/
@Service
public class PeaScoreServiceImpl extends ServiceImpl<PeaScoreMapper, PeaScore>
    implements PeaScoreService {

    @Autowired
    private PeaScoreMapper scoreMapper;

    @Autowired
    private SysUserMapper userMapper;


    /**
     * 评议农户的数据权限
     * @param scoreId 评议Id
     */
    @Override
    public boolean checkScopeData(Long scoreId){
        PeaScore peaScore = scoreMapper.selectById(scoreId);
        if(StringUtils.isNotEmpty(peaScore)){
            Long userId = SecurityUtils.getUserId();
            if(StringUtils.isNotEmpty(userId) && userId.equals(peaScore.getSenatorId())){
                return true;
            }
        }
        return false;
    }

    /**
     * 根据户主ID查询相关评分个数
     * @param hzId 户主ID
     * @return 已经评分的个数
     */
    @Override
    public int checkScoreNumByHzId(Long hzId) {
        LambdaQueryWrapper<PeaScore> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PeaScore::getHzId, hzId).eq(PeaScore::getFinishStatus, FinishStatus.YES);
        return Math.toIntExact(scoreMapper.selectCount(queryWrapper));
    }

    /**
     * 分页查询
     * @param hzBaseDto 户主信息
     * @return 查询结果
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<PeaScoreDto> selectListPage(PeaHzBaseDto hzBaseDto) {
        Long userId = SecurityUtils.getUserId();
        hzBaseDto.setUserId(userId);
        return scoreMapper.selectListPage(hzBaseDto);
    }


    /**
     * 将户主信息生成评分
     * @param hzId 户主ID
     * @return 随机分配结果
     */
    @Override
    @Transactional
    public int randomAllotSenator(Long hzId) {
        // 将可以看到该部门的所有评议员选择出来
        SysUser user = SecurityUtils.getUser();
        Long deptId = user.getDeptId();
        String ancestors = "'" + user.getDept().getAncestors()  + "," + user.getDeptId() + "'";
        // 获取评议员的ID
        List<SysUser> list = userMapper.selectSenatorUserByRoleId(deptId, ancestors);

        if(list.size() < 5){
            throw new ServiceException("评议员的个数不到5个请联系管理员进行添加！");
        }
        // 选取前五个评议员进行评分
        int rows = 0;
        for (int i = 0; i < 5; i++) {
            PeaScore peaScore = new PeaScore();
            peaScore.setSenatorId(list.get(i).getUserId());
            peaScore.setHzId(hzId);
            int insert = scoreMapper.insert(peaScore);
            rows = rows + insert;
        }
        return rows;
    }



    /**
     * 通过户主ID查询评议信息
     * @param hzId 户主ID
     * @return 返回结果
     */
    @Override
    public List<PeaScoreDto> selectScoreByHzId(Long hzId) {
        return scoreMapper.selectScoreByHzId(hzId);
    }


    // 根据户主ID，删除对应的哦评分
    @Override
    public int deleteScoreByHzId(Long[] hzIds){
        return scoreMapper.deleteScoreByHzId(hzIds);
    }

}




